﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using STX.Domain;
using System.Text;

namespace STX.Admin.Models.BizActivity
{
    public class StepModel : BaseAddModel
    {
        public StepModel(StepEnum step)
        {
            Step = step;

        }
        public StepEnum Step { get; set; }
        public string GetHtml
        {
            get
            {
                var html = new StringBuilder();
                var fieldInfo = Step.GetType().GetField(Step.ToString());
                var cass = (StepAttribute[])fieldInfo.GetCustomAttributes(typeof(StepAttribute), false);
                var casu = (MyUserAttribute[])fieldInfo.GetCustomAttributes(typeof(MyUserAttribute), false);
                if (cass.Length <= 0 || casu.Length <= 0)
                {
                    throw new Exception("流程步骤配置出了问题");
                }
                var Role = casu[0].Role;
                //检查权限

                var behave = casu[0].Behave;
                var behaveFields = typeof(UserBehave).GetFields();
                html.Append("<select name=\"UserBehave\">");
                foreach (var field in behaveFields)
                {
                    if (field.IsSpecialName) continue;
                    var value = (UserBehave)Enum.Parse(typeof(UserBehave), field.Name);
                    if ((value & behave) > 0)
                    {
                        html.AppendFormat("<option value=\"{0}\">{1}</option>", (int)value, value.ToString());
                    }
                }
                html.Append("</select>");

                //退回给上一级
                if ((UserBehave.审核失败 & behave) > 0)
                {
                    var prevStep = cass[0].PrevStep;
                    html.Append("<span style=\"display:none\" id=\"span_backto\"> 退回到 <select name=\"Step\">");
                    foreach (var field in typeof(StepEnum).GetFields())
                    {
                        if (field.IsSpecialName) continue;
                        var value = (StepEnum)Enum.Parse(typeof(StepEnum), field.Name);
                        if ((value & prevStep) > 0)
                        {
                            html.AppendFormat("<option value=\"{0}\">{1}</option>", (int)value, value.ToString());
                        }
                    }
                    html.Append("</select></span>");
                    html.Append(@"<script type=""text/javascript"">
                                    $(function() {
                                         $(""select[name='UserBehave']"").click(function(){
                                            var $this=$(this);
                                            if($this.val()=="+(int)UserBehave.审核失败+ @"){
                                                $(""#span_backto"").show(); 
                                            }else{
                                                $(""#span_backto"").hide();
                                            }
                                         });
                                    });
                                 </script>");
                }

                if ((UserBehave.审核失败 & behave) > 0 || (UserBehave.审核成功 & behave) >0)
                {
                    html.AppendFormat(" 意见：<input name=\"Comment\" style=\"border:#ff6699 1px solid\" class=\"textInput\" type=\"text\" size=\"40\" alt=\"\"/>");
                }
                return html.ToString();
            }
        }
    }
}